//
//  HNSH_CartShopCountView.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/2.
//

#import "HNSH_CartShopCountView.h"

@interface HNSH_CartShopCountView ()

/* addButton */
@property (nonatomic, strong) UIButton *addButton;
/* textField */
@property (nonatomic, strong) UITextField *textField;
/* jianButton */
@property (nonatomic, strong) UIButton *jianButton;

@end

@implementation HNSH_CartShopCountView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self configUI];
    }
    return self;
}

- (void)setCount:(NSInteger)count {
    _count = count;
    self.textField.text = [NSString stringWithFormat:@"%ld",count];
}

- (void)configUI {
    [self addSubview:self.textField];
    [self addSubview:self.jianButton];
    [self addSubview:self.addButton];
    
    WeakSelf(weakSelf);
    [[self.jianButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.count <= 1) {
            return;
        }
        weakSelf.count --;
        if (weakSelf.countBlock) {
            weakSelf.countBlock(weakSelf.count);
        }
    }];
    
    [[self.addButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.count ++;
        if (weakSelf.countBlock) {
            weakSelf.countBlock(weakSelf.count);
        }
    }];
}

#pragma mark lazy
- (UIButton *)addButton {
    if (!_addButton) {
        _addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _addButton.frame = CGRectMake(self.width-self.height, 0, self.height, self.height);
        [_addButton setBackgroundImage:[UIImage imageNamed:@"goods_detail_count_add"] forState:UIControlStateNormal];
    }
    return _addButton;
}

- (UIButton *)jianButton {
    if (!_jianButton) {
        _jianButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _jianButton.frame = CGRectMake(0, 0, self.height, self.height);
        [_jianButton setBackgroundImage:[UIImage imageNamed:@"goods_detail_count_jian"] forState:UIControlStateNormal];
    }
    return _jianButton;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(self.height, 0, self.width-36, self.height)];
        _textField.textColor = [UIColor appBlackLabelColor];
        _textField.font = [UIFont systemFontOfSize:12];
        _textField.text = @"1";
        _textField.textAlignment = NSTextAlignmentCenter;
        _textField.userInteractionEnabled = NO;
    }
    return _textField;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
